from django.contrib import admin
from .models import StudentInfo, TeacherInfo, CourseInfo, ClassCourse, ClassInfo
import pandas as pd
from django.http import HttpResponse

# 浏览器标签
admin.site.site_title = "井冈山经贸学校信息管理系统"
# 后台首页
admin.site.index_title = "井冈山经贸学校信息管理系统"
# 页面左上角
admin.site.site_header = "井冈山经贸学校信息管理系统"


def export_to_csv(modeladmin, request, queryset):
    """
    导出所选的数据为csv文件
    """
    # 获取字段名
    columns = [field.name for field in TeacherInfo._meta.fields]

    # 创建DataFrame
    df = pd.DataFrame(list(queryset.values(*columns)), columns=columns)

    # 将身份证号码字段转换为字符串并格式化
    if "id_card" in df.columns:
        df["id_card"] = df["id_card"].astype(str).apply(lambda x: f"{x:0>18}")

    # 创建http响应
    response = HttpResponse(content_type="text/csv")
    response["Content-Disposition"] = "attachment; filename=evaluation_list.csv"

    # 写入csv文件
    df.to_csv(path_or_buf=response, index=False, encoding="gbk")

    return response


export_to_csv.short_description = "导出所选的数据为csv文件"


@admin.register(StudentInfo)
class StudentInfoAdmin(admin.ModelAdmin):
    list_display = (
        "student_id",
        "name",
        "age",
        "gender",
        "id_card",
        "tel",
        "class_name",
        "class_header",
        "course_count",
        "eval_count",
        "remain",
    )
    readonly_fields = (
        "age",
        "gender",
        "class_name",
        "class_header_id",
        "class_header",
        "course_count",
        "eval_count",
        "remain",
    )
    search_fields = (
        "student_id",
        "name",
        "gender",
        "id_card",
        "tel",
        "class_name",
        "class_header",
    )
    actions = [export_to_csv]


@admin.register(TeacherInfo)
class TeacherInfoAdmin(admin.ModelAdmin):
    list_display = (
        "teacher_id",
        "name",
        "age",
        "gender",
        "id_card",
        "tel",
        "eval_count",
        "score",
    )
    readonly_fields = (
        "age",
        "gender",
        "course_count",
        "eval_count",
        "score",
    )
    search_fields = (
        "teacher_id",
        "name",
        "gender",
        "id_card",
        "tel",
    )
    actions = [export_to_csv]


@admin.register(CourseInfo)
class CourseInfoAdmin(admin.ModelAdmin):
    list_display = (
        "course_id",
        "course_name",
        "credit",
    )
    search_fields = (
        "course_name",
        "credit",
    )
    actions = [export_to_csv]


@admin.register(ClassCourse)
class ClassCourseAdmin(admin.ModelAdmin):
    list_display = (
        "class_course_id",
        "class_name",
        "course_name",
        "teacher_name",
    )
    readonly_fields = (
        "class_name",
        "course_name",
        "teacher_name",
    )
    search_fields = (
        "class_name",
        "course_name",
        "teacher_name",
    )
    actions = [export_to_csv]


@admin.register(ClassInfo)
class ClassInfoAdmin(admin.ModelAdmin):
    list_display = (
        "class_id",
        "class_name",
        "grade",
        "class_header",
        "course_count",
        "student_count",
        "is_eval",
        "eval_count",
        "remain",
    )
    readonly_fields = (
        "class_header",
        "course_count",
        "student_count",
        "eval_count",
        "remain",
    )
    search_fields = (
        "class_name",
        "grade",
        "class_header",
    )
    actions = [export_to_csv]
